/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Config.java
 *
 * Created on 01-03-2011, 08:50:59
 */

package GUI;

import entity.Server;
import interact.connect;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author UnderGround
 */
public class Config extends javax.swing.JPanel {

    /** Creates new form Config */
    public Config(){
         try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(frmLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(frmLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(frmLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(frmLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        txtDatabaseName = new javax.swing.JTextField();
        txtServer = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtUser = new javax.swing.JTextField();
        txtPort = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        btnDefault = new javax.swing.JButton();
        txtPassword = new javax.swing.JPasswordField();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Server config"));
        setMinimumSize(new java.awt.Dimension(280, 200));
        setPreferredSize(new java.awt.Dimension(350, 200));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel6.setText("Database Name");
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, 90, -1));
        add(txtDatabaseName, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, 160, -1));
        add(txtServer, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 60, 80, -1));

        jLabel7.setText("Server name");
        add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 60, 80, -1));

        jLabel8.setText("User name");
        add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, 70, 20));

        jLabel9.setText("Password");
        add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 130, 80, -1));
        add(txtUser, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 90, 160, -1));
        add(txtPort, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 60, 40, -1));

        jLabel10.setText("Port");
        add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 60, 30, -1));

        btnSave.setText("Save config");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        add(btnSave, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 160, -1, -1));

        btnDefault.setText("Default");
        btnDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDefaultActionPerformed(evt);
            }
        });
        add(btnDefault, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 160, 70, -1));
        add(txtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 120, 160, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
         SaveConfig();
        JOptionPane.showMessageDialog(null, "server config successful");
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDefaultActionPerformed
            txtDatabaseName.setText("LibraryManagement");
            txtPassword.setText("1");
            txtPort.setText("1433");
            txtServer.setText("localhost");
            txtUser.setText("sa");

    }//GEN-LAST:event_btnDefaultActionPerformed
 
    private void SaveConfig(){
        String pass = new String(txtPassword.getPassword());
          try {
            File file= new File("server.config");
            if(!file.exists()){
                file.createNewFile();
                System.out.println("create file successful");
                ObjectOutputStream fos = new ObjectOutputStream(new FileOutputStream(file));

            Server sv = new Server(txtServer.getText(), Integer.parseInt(txtPort.getText()), txtUser.getText(),pass, txtDatabaseName.getText());
            fos.writeObject(sv);
            System.out.println(pass);
            fos.close();
            }else{
             ObjectOutputStream fos = new ObjectOutputStream(new FileOutputStream(file));

            Server sv = new Server(txtServer.getText(), Integer.parseInt(txtPort.getText()), txtUser.getText(),pass, txtDatabaseName.getText());
            fos.writeObject(sv);
            System.out.println(pass);
            fos.close();
            connect.connection();
            }
          interact.connect.configServer();
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDefault;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField txtDatabaseName;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtServer;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables

}
